package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.CBase;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.model.sys.User;
import com.weipai.springboot.module.controller.CommonController;
import com.weipai.springboot.module.mapper.CBaseMapper;
import com.weipai.springboot.module.service.CommonService;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Auther: gusl
 * @Date: 2019/7/22 11:35
 * @Description:
 */
@Service
public class CommonServiceImpl implements CommonService {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private CBaseMapper cBaseMapper;
    @Autowired
    private SessionUserUtil sessionUserUtil;


    /**
     *
     * 基本信息操作新增
     * @author gusl
     * @date 2019/7/22 11:51
     * @param  [cBase]
     * @return com.weipai.springboot.model.api.Response
     */

    @Override
    public Response saveCbaer(CBase cBase) {
        Response response = new Response();
        try {
            CBase cBase1 = new CBase();
            cBase1.setValid(true);
            CBase cBase2 = cBaseMapper.selectOne(cBase1);
            if(cBase2!=null){
                cBase2.setValid(false);
                cBaseMapper.updateByPrimaryKeySelective(cBase2);
            }
            User user = sessionUserUtil.getUser();
            String customerId = sessionUserUtil.getAdminAttr().getAdminLoginName();
            cBase.setCreateName(customerId);
            cBase.setUpdateName(customerId);
            cBase.setValid(true);
            cBaseMapper.insertSelective(cBase);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("基本信息操作：e"+e.getMessage());
            response.setCode(RetCode.REQUEST_ERROR);
            response.setMsg(RetCode.REQUEST_ERROR.getMsg());
            return response;
        }
        response.setCode(RetCode.SUCCESS);
        response.setMsg(RetCode.SUCCESS.getMsg());
        return response;
    }

    /**
     *
     * 查询基本设置详情
     * @author gusl
     * @date 2019/7/22 13:31
     * @param  []
     * @return com.weipai.springboot.model.api.Response
     */

    @Override
    public Response searchCbaer() {
        Response response = new Response();
        CBase cBase1 = new CBase();
        cBase1.setValid(true);
        CBase cBase2= new CBase();
         cBase2 = cBaseMapper.selectOne(cBase1);
        response.setCode(RetCode.SUCCESS);
        response.setMsg(RetCode.SUCCESS.getMsg());
        response.setData(cBase2);
        return response;
    }
}
